package com.apollographql.apollo.internal.interceptor;

import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.ScalarTypeAdapters;
import com.apollographql.apollo.api.cache.http.HttpCache;
import com.apollographql.apollo.api.internal.ApolloLogger;
import com.apollographql.apollo.api.internal.ResponseFieldMapper;
import com.apollographql.apollo.cache.normalized.internal.ResponseNormalizer;
import com.apollographql.apollo.exception.ApolloException;
import com.apollographql.apollo.exception.ApolloHttpException;
import com.apollographql.apollo.exception.ApolloParseException;
import com.apollographql.apollo.http.OkHttpExecutionContext;
import com.apollographql.apollo.interceptor.ApolloInterceptor;
import com.apollographql.apollo.interceptor.ApolloInterceptorChain;
import com.apollographql.apollo.response.OperationResponseParser;
import java.io.Closeable;
import java.util.Map;
import java.util.concurrent.Executor;
import okhttp3.Response;

public final class ApolloParseInterceptor implements ApolloInterceptor {
    volatile boolean disposed;
    private final HttpCache httpCache;
    private final ApolloLogger logger;
    private final ResponseNormalizer<Map<String, Object>> normalizer;
    private final ResponseFieldMapper responseFieldMapper;
    private final ScalarTypeAdapters scalarTypeAdapters;

    public ApolloParseInterceptor(HttpCache httpCache2, ResponseNormalizer<Map<String, Object>> normalizer2, ResponseFieldMapper responseFieldMapper2, ScalarTypeAdapters scalarTypeAdapters2, ApolloLogger logger2) {
        this.httpCache = httpCache2;
        this.normalizer = normalizer2;
        this.responseFieldMapper = responseFieldMapper2;
        this.scalarTypeAdapters = scalarTypeAdapters2;
        this.logger = logger2;
    }

    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
    public void interceptAsync(final InterceptorRequest request, ApolloInterceptorChain chain, Executor dispatcher, final CallBack callBack) {
        if (!this.disposed) {
            chain.proceedAsync(request, dispatcher, new CallBack() {
                /* class com.apollographql.apollo.internal.interceptor.ApolloParseInterceptor.AnonymousClass1 */

                @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                public void onResponse(InterceptorResponse response) {
                    try {
                        if (!ApolloParseInterceptor.this.disposed) {
                            callBack.onResponse(ApolloParseInterceptor.this.parse(request.operation, response.httpResponse.get()));
                            callBack.onCompleted();
                        }
                    } catch (ApolloException e) {
                        onFailure(e);
                    }
                }

                @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                public void onFailure(ApolloException e) {
                    if (!ApolloParseInterceptor.this.disposed) {
                        callBack.onFailure(e);
                    }
                }

                @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                public void onCompleted() {
                }

                @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                public void onFetch(FetchSourceType sourceType) {
                    callBack.onFetch(sourceType);
                }
            });
        }
    }

    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
    public void dispose() {
        this.disposed = true;
    }

    /* access modifiers changed from: package-private */
    public InterceptorResponse parse(Operation operation, Response httpResponse) throws ApolloHttpException, ApolloParseException {
        HttpCache httpCache2;
        String cacheKey = httpResponse.request().header("X-APOLLO-CACHE-KEY");
        if (httpResponse.isSuccessful()) {
            try {
                OperationResponseParser parser = new OperationResponseParser(operation, this.responseFieldMapper, this.scalarTypeAdapters, this.normalizer);
                OkHttpExecutionContext httpExecutionContext = new OkHttpExecutionContext(httpResponse);
                com.apollographql.apollo.api.Response parsedResponse = parser.parse(httpResponse.body().source());
                com.apollographql.apollo.api.Response parsedResponse2 = parsedResponse.toBuilder().fromCache(httpResponse.cacheResponse() != null).executionContext(parsedResponse.getExecutionContext().plus(httpExecutionContext)).build();
                if (parsedResponse2.hasErrors() && (httpCache2 = this.httpCache) != null) {
                    httpCache2.removeQuietly(cacheKey);
                }
                return new InterceptorResponse(httpResponse, parsedResponse2, this.normalizer.records());
            } catch (Exception rethrown) {
                this.logger.e(rethrown, "Failed to parse network response for operation: %s", operation.name().name());
                closeQuietly(httpResponse);
                HttpCache httpCache3 = this.httpCache;
                if (httpCache3 != null) {
                    httpCache3.removeQuietly(cacheKey);
                }
                throw new ApolloParseException("Failed to parse http response", rethrown);
            }
        } else {
            this.logger.e("Failed to parse network response: %s", httpResponse);
            throw new ApolloHttpException(httpResponse);
        }
    }

    private static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception e) {
            }
        }
    }
}
